package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.NecessaryVariableAddDTO;
import org.jsola.hr.dto.NecessaryVariableBatchDTO;
import org.jsola.hr.dto.NecessaryVariableUpdateDTO;
import org.jsola.hr.query.NecessaryVariableQuery;
import org.jsola.hr.service.INecessaryVariableService;
import org.jsola.hr.vo.NecessaryVariableListVO;
import org.jsola.hr.vo.NecessaryVariableVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 员工必填信息关联变量
 *
 * @author zhr
 */
@RestController("hrNecessaryVariableController")
@RequestMapping(HrConstants.API_PATH + "/v1/necessaryVariables")
public class NecessaryVariableController implements INecessaryVariableControllerAPI {

    @Autowired
    private INecessaryVariableService necessaryVariableService;

    @Override
    @PostMapping
    public Result<NecessaryVariableVO> save(@Valid @RequestBody NecessaryVariableAddDTO necessaryVariableAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工必填信息关联变量权限校验

        return Result.success(
                necessaryVariableService.save(necessaryVariableAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody NecessaryVariableUpdateDTO necessaryVariableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工必填信息关联变量权限校验

        int count = necessaryVariableService.update(necessaryVariableUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody NecessaryVariableBatchDTO necessaryVariableBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<NecessaryVariableAddDTO> necessaryVariableAddDTOList = necessaryVariableBatchDTO.getNecessaryVariableAddDTOList();
        if (!CollectionUtils.isEmpty(necessaryVariableAddDTOList)) {
            necessaryVariableService.batchSave(necessaryVariableAddDTOList,tokenUser);
        }
        // 修改
        List<NecessaryVariableUpdateDTO> necessaryVariableUpdateDTOList = necessaryVariableBatchDTO.getNecessaryVariableUpdateDTOList();
        if (!CollectionUtils.isEmpty(necessaryVariableUpdateDTOList)) {
            necessaryVariableUpdateDTOList.parallelStream().forEach(
                    necessaryVariableUpdateDTO -> {
                        necessaryVariableService.update(necessaryVariableUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deleteNecessaryVariableIdList = necessaryVariableBatchDTO.getDeleteNecessaryVariableIdList();
        if (!CollectionUtils.isEmpty(deleteNecessaryVariableIdList)) {
            necessaryVariableService.deleteByIds(tokenUser,deleteNecessaryVariableIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{necessaryVariableId}")
    public Result<Integer> delete(@PathVariable Long necessaryVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工必填信息关联变量权限校验

        int count = necessaryVariableService.deleteByIds(tokenUser, necessaryVariableId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] necessaryVariableIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工必填信息关联变量权限校验

        int count = necessaryVariableService.deleteByIds(tokenUser, necessaryVariableIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{necessaryVariableId}")
    public Result<NecessaryVariableVO> findById(@PathVariable Long necessaryVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工必填信息关联变量权限校验

        return Result.success(
            necessaryVariableService.selectById(necessaryVariableId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<NecessaryVariableListVO>> listAll(@Valid NecessaryVariableQuery necessaryVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工必填信息关联变量权限校验

        return Result.success(
            necessaryVariableService.select(necessaryVariableQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<NecessaryVariableListVO>> page(@Valid NecessaryVariableQuery necessaryVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            necessaryVariableService.selectPage(necessaryVariableQuery, tokenUser.getSiteId())
        );
    }


}

